package com.nbwyctf.helper.utils;

import com.nbwyctf.helper.enums.ErrorCode;
import com.nbwyctf.helper.exception.BusinessException;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtils {
    private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class);
    public static Sheet excelToList(InputStream inputStream) {
        List<List> list = new ArrayList<>();
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(inputStream);
            inputStream.close();
            Sheet sheet = workbook.getSheetAt(0);
            return sheet;
        } catch (BusinessException e) {
            throw new BusinessException(ErrorCode.NOT_EXCEL_FILE);
        } catch (Exception e) {
            logger.error("parse excel file error :", e);
        }
        return null;
    }

    public static void setHeaders(HSSFRow row, String[] headers) {
        for (int i = 0; i < headers.length; i++) {
            row.createCell(i).setCellValue(headers[i]);
        }
    }
}
